#include "userinfowidget.h"
#include <QCursor>
#include <QGridLayout>
#include <QMessageBox>

#include "mainwidget.h"
#include "model/datacenter.h"


UserInfoWidget::UserInfoWidget(const UserInfo& userInfo,QWidget* parent)
    :userInfo(userInfo),QDialog(parent)
{
    //设置基本属性
    this->setFixedSize(400,200);
    this->setWindowTitle("用户详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    //避免内存泄露
    this->setAttribute(Qt::WA_DeleteOnClose);
    // 移动到鼠标位置
    this->move(QCursor::pos());

    //创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setVerticalSpacing(10);
    layout->setHorizontalSpacing(20);
    layout->setContentsMargins(40,20,0,0);
    layout->setAlignment(Qt::AlignTop);
    this->setLayout(layout);

    //添加头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(75,75);
    avatarBtn->setIconSize(QSize(75,75));
    avatarBtn->setIcon(userInfo.avatar);

    QString labelStyle = "QLabel { font-weight: 800; padding-left: 20px;}";
    QString btnStyle = "QPushButton { border: 1px solid rgb(100, 100, 100); border-radius: 5px; background-color: rgb(240, 240, 240); }";
    btnStyle += "QPushButton:pressed { background-color: rgb(205, 205, 205); }";

    int width = 80;
    int height = 30;

    //添加用户序号
    idTag = new QLabel();
    idTag->setText("序号");
    idTag->setStyleSheet(labelStyle);
    idTag->setFixedSize(width,height);
    idTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    idLabel = new QLabel();
    idLabel->setText(userInfo.userId);
    idLabel->setFixedSize(width,height);
    idLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    //添加用户昵称
    nameTag = new QLabel();
    nameTag->setText("昵称");
    nameTag->setStyleSheet(labelStyle);
    nameTag->setFixedSize(width,height);
    nameTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    nameLabel = new QLabel();
    nameLabel->setText(userInfo.nickname);
    nameLabel->setFixedSize(width,height);
    nameLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    //添加用户电话
    phoneTag = new QLabel();
    phoneTag->setText("电话");
    phoneTag->setStyleSheet(labelStyle);
    phoneTag->setFixedSize(width,height);
    phoneTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    phoneLabel = new QLabel();
    phoneLabel->setText(userInfo.phone);
    phoneLabel->setFixedSize(width,height);
    phoneLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    //添加 好友申请按钮 发送消息按钮  删除好友按钮
    applyBtn = new QPushButton();
    applyBtn->setText("申请好友");
    applyBtn->setFixedSize(80,30);
    applyBtn->setStyleSheet(btnStyle);
    applyBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    sendMessageBtn = new QPushButton();
    sendMessageBtn->setText("发送消息");
    sendMessageBtn->setFixedSize(80,30);
    sendMessageBtn->setStyleSheet(btnStyle);
    sendMessageBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setFixedSize(80,30);
    deleteFriendBtn->setStyleSheet(btnStyle);
    deleteFriendBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);

    //添加到布局管理器中
    layout->addWidget(avatarBtn,0,0,3,1);

    layout->addWidget(idTag,0,1);
    layout->addWidget(idLabel,0,2);

    layout->addWidget(nameTag,1,1);
    layout->addWidget(nameLabel,1,2);

    layout->addWidget(phoneTag,2,1);
    layout->addWidget(phoneLabel,2,2);

    layout->addWidget(applyBtn,3,0);
    layout->addWidget(sendMessageBtn,3,1);
    layout->addWidget(deleteFriendBtn,3,2);


    // 判断是不是好友，如果不是就将发送消息和删除好友按钮给禁用掉，反之禁用掉添加好友
    DataCenter* dataCenter = DataCenter::getInstance();
    auto* myFriend = dataCenter->findFriendById(this->userInfo.userId);
    if(myFriend == nullptr) {
        // 说明不是好友关系
        sendMessageBtn->setEnabled(false);
        deleteFriendBtn->setEnabled(false);
    }else {
        // 是好友关系
        applyBtn->setEnabled(false);
    }

    // 初始化信号槽
    initSignalSlot();
}

void UserInfoWidget::initSignalSlot()
{
    connect(sendMessageBtn,&QPushButton::clicked,this,[=](){
        // 将窗口放到第一个，并且加载会话信息
        MainWidget* mainWidget = MainWidget::getInstance();
        mainWidget->switchSession(userInfo.userId);

        // 将打开好友信息的窗口关闭掉
        this->close();
    });


    connect(deleteFriendBtn,&QPushButton::clicked,this,&UserInfoWidget::clickDeleteFriendBtn);
    connect(applyBtn,&QPushButton::clicked,this,&UserInfoWidget::clickApplyFriendBtn);
}

void UserInfoWidget::clickDeleteFriendBtn()
{
    auto result = QMessageBox::warning(this,"确定删除","确定删除好友？",QMessageBox::Ok | QMessageBox::Cancel);
    if(result != QMessageBox::Ok) {
        LOG() << "删除好友取消";
        return;
    }

    // 发送网络，服务器也要从数据库删除
    DataCenter* dataCenter = DataCenter::getInstance();
    dataCenter->deleteFriendAsync(this->userInfo.userId);

    this->close();
}

void UserInfoWidget::clickApplyFriendBtn()
{
    DataCenter* dataCenter = DataCenter::getInstance();
    dataCenter->addFriendApplyAsync(userInfo.userId);

    // 将打开好友信息的窗口关闭掉
    this->close();
}
